<template>
  <div id="chatManagePage">
    <!-- 搜索表单 -->
    <div class="search-container">
      <div class="search-header">
        <div class="search-title">
          <span class="title-icon">💬</span>
          <span>聊天管理</span>
        </div>
        <div class="search-subtitle">管理系统中的所有聊天记录</div>
      </div>
      <a-form layout="inline" :model="searchParams" @finish="doSearch" class="search-form">
        <a-form-item label="消息内容" class="form-item">
          <a-input
            v-model:value="searchParams.message"
            placeholder="请输入消息内容"
            prefix="💭"
            class="search-input"
          />
        </a-form-item>
        <a-form-item label="消息类型" class="form-item">
          <a-select
            v-model:value="searchParams.messageType"
            placeholder="选择消息类型"
            style="width: 180px"
            class="search-select"
          >
            <a-select-option value="">全部类型</a-select-option>
            <a-select-option value="user_message">👤 用户消息</a-select-option>
            <a-select-option value="ai_message">🤖 AI消息</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="应用ID" class="form-item">
          <a-input
            v-model:value="searchParams.appId"
            placeholder="请输入应用ID"
            prefix="🚀"
            class="search-input"
          />
        </a-form-item>
        <a-form-item label="用户ID" class="form-item">
          <a-input
            v-model:value="searchParams.userId"
            placeholder="请输入用户ID"
            prefix="👤"
            class="search-input"
          />
        </a-form-item>
        <a-form-item class="form-item">
          <a-button type="primary" html-type="submit" class="search-btn">
            <template #icon>
              <span>🔍</span>
            </template>
            搜索记录
          </a-button>
        </a-form-item>
      </a-form>
    </div>


    <!-- 表格容器 -->
    <div class="table-container">
      <a-table
        :columns="columns"
        :data-source="data"
        :pagination="pagination"
        @change="doTableChange"
        :scroll="{ x: 1400 }"
        class="chat-table"
      >
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'message'">
          <a-tooltip :title="record.message">
            <div class="message-text">{{ record.message }}</div>
          </a-tooltip>
        </template>
        <template v-else-if="column.dataIndex === 'messageType'">
          <a-tag :color="record.messageType === 'user_message' ? 'blue' : 'green'">
            {{ record.messageType === 'user_message' ? '用户消息' : 'AI消息' }}
          </a-tag>
        </template>
        <template v-else-if="column.dataIndex === 'createTime'">
          {{ formatTime(record.createTime) }}
        </template>
        <template v-else-if="column.key === 'action'">
          <a-space>
            <a-button type="primary" size="small" @click="viewAppChat(record.appId)">
              查看对话
            </a-button>
            <a-popconfirm title="确定要删除这条消息吗？" @confirm="deleteMessage(record.id)">
              <a-button danger size="small">删除</a-button>
            </a-popconfirm>
          </a-space>
        </template>
      </template>
      </a-table>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { computed, onMounted, reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { message } from 'ant-design-vue'
import { listAllChatHistoryByPageForAdmin } from '@/api/chatHistoryController'
import { formatTime } from '@/utils/time'

const router = useRouter()

const columns = [
  {
    title: 'ID',
    dataIndex: 'id',
    width: 80,
    fixed: 'left',
  },
  {
    title: '消息内容',
    dataIndex: 'message',
    width: 300,
  },
  {
    title: '消息类型',
    dataIndex: 'messageType',
    width: 100,
  },
  {
    title: '应用ID',
    dataIndex: 'appId',
    width: 80,
  },
  {
    title: '用户ID',
    dataIndex: 'userId',
    width: 80,
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    width: 160,
  },
  {
    title: '操作',
    key: 'action',
    width: 180,
    fixed: 'right',
  },
]

// 数据
const data = ref<API.ChatHistory[]>([])
const total = ref(0)

// 搜索条件
const searchParams = reactive<API.ChatHistoryQueryRequest>({
  pageNum: 1,
  pageSize: 10,
})

// 获取数据
const fetchData = async () => {
  try {
    const res = await listAllChatHistoryByPageForAdmin({
      ...searchParams,
    })
    if (res.data.data) {
      data.value = res.data.data.records ?? []
      total.value = res.data.data.total ?? 0
    } else {
      message.error('获取数据失败，' + res.data.message)
    }
  } catch (error) {
    console.error('获取数据失败：', error)
    message.error('获取数据失败')
  }
}

// 页面加载时请求一次
onMounted(() => {
  fetchData()
})

// 分页参数
const pagination = computed(() => {
  return {
    current: searchParams.pageNum ?? 1,
    pageSize: searchParams.pageSize ?? 10,
    total: total.value,
    showSizeChanger: true,
    showTotal: (total: number) => `共 ${total} 条`,
  }
})

// 表格变化处理
const doTableChange = (page: { current: number; pageSize: number }) => {
  searchParams.pageNum = page.current
  searchParams.pageSize = page.pageSize
  fetchData()
}

// 搜索
const doSearch = () => {
  // 重置页码
  searchParams.pageNum = 1
  fetchData()
}

// 查看应用对话
const viewAppChat = (appId: number | undefined) => {
  if (appId) {
    router.push(`/app/chat/${appId}`)
  }
}

// 删除消息
const deleteMessage = async (id: number | undefined) => {
  if (!id) return

  try {
    // 注意：这里需要后端提供删除对话历史的接口
    // 目前先显示成功，实际实现需要调用删除接口
    message.error('目前不支持管理员删除用户对话记录')
    // 刷新数据
    fetchData()
  } catch (error) {
    console.error('删除失败：', error)
    message.error('删除失败')
  }
}
</script>

<style scoped>
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap');

/* 统一动画变量 */
:root {
  --animation-duration-fast: 0.2s;
  --animation-duration-normal: 0.3s;
  --animation-duration-slow: 0.5s;
  --animation-easing: cubic-bezier(0.4, 0, 0.2, 1);
  --animation-easing-bounce: cubic-bezier(0.68, -0.55, 0.265, 1.55);
}

/* 动画定义 */
@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes slideInLeft {
  from {
    opacity: 0;
    transform: translateX(-30px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

#chatManagePage {
  padding: 32px;
  background: rgba(248, 250, 252, 0.8);
  min-height: 100vh;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  animation: fadeInUp 0.8s var(--animation-easing);
}

/* 搜索容器样式 */
.search-container {
  background: rgba(255, 255, 255, 0.98);
  border-radius: 20px;
  padding: 28px;
  margin-bottom: 24px;
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.08);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.3);
  animation: slideInLeft 0.6s var(--animation-easing);
  position: relative;
  overflow: hidden;
}

.search-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: linear-gradient(90deg, #10b981 0%, #059669 50%, #047857 100%);
  border-radius: 20px 20px 0 0;
}

.search-header {
  text-align: center;
  margin-bottom: 24px;
}

.search-title {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  font-size: 22px;
  font-weight: 700;
  color: #1a202c;
  margin-bottom: 6px;
}

.title-icon {
  font-size: 24px;
}

.search-subtitle {
  font-size: 14px;
  color: #64748b;
  font-weight: 500;
  opacity: 0.8;
}

.search-form {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 16px;
}

.form-item {
  margin-bottom: 0;
}

.search-input {
  width: 180px;
  height: 40px;
  border-radius: 12px;
  border: 2px solid rgba(226, 232, 240, 0.8);
  transition: all var(--animation-duration-normal) var(--animation-easing);
  font-weight: 500;
}

.search-input:hover {
  border-color: rgba(16, 185, 129, 0.4);
  box-shadow: 0 0 0 3px rgba(16, 185, 129, 0.05);
}

.search-input:focus {
  border-color: #10b981;
  box-shadow: 0 0 0 3px rgba(16, 185, 129, 0.15);
}

.search-select {
  height: 40px;
  border-radius: 12px;
}

.search-btn {
  height: 40px;
  padding: 0 24px;
  border-radius: 12px;
  font-weight: 600;
  background: linear-gradient(135deg, #10b981 0%, #059669 100%);
  border: none;
  box-shadow: 0 4px 15px rgba(16, 185, 129, 0.3);
  transition: all var(--animation-duration-normal) var(--animation-easing);
}

.search-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(16, 185, 129, 0.4);
  background: linear-gradient(135deg, #059669 0%, #047857 100%);
}

/* 表格容器样式 */
.table-container {
  background: rgba(255, 255, 255, 0.98);
  border-radius: 20px;
  padding: 24px;
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.08);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.3);
  animation: fadeInUp 0.6s var(--animation-easing) 0.2s both;
  position: relative;
  overflow: hidden;
}

.table-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: linear-gradient(90deg, #8b5cf6 0%, #a855f7 50%, #c084fc 100%);
  border-radius: 20px 20px 0 0;
}

/* 表单样式 */
:deep(.ant-form) {
  background: rgba(255, 255, 255, 0.95);
  padding: 24px;
  border-radius: 16px;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.3);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.08);
  margin-bottom: 24px;
  animation: slideInLeft 0.6s var(--animation-easing);
}

:deep(.ant-input) {
  border-radius: 8px;
  border: 1px solid rgba(209, 213, 219, 0.8);
  transition: all var(--animation-duration-normal) var(--animation-easing);
  font-weight: 500;
}

:deep(.ant-input:hover) {
  border-color: #667eea;
  box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.1);
}

:deep(.ant-input:focus) {
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.15);
}

:deep(.ant-select) {
  border-radius: 8px;
}

:deep(.ant-select .ant-select-selector) {
  border-radius: 8px;
  border: 1px solid rgba(209, 213, 219, 0.8);
  transition: all var(--animation-duration-normal) var(--animation-easing);
  font-weight: 500;
}

:deep(.ant-select:hover .ant-select-selector) {
  border-color: #667eea;
  box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.1);
}

:deep(.ant-select-focused .ant-select-selector) {
  border-color: #667eea !important;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.15) !important;
}

:deep(.ant-btn-primary) {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  border-radius: 8px;
  font-weight: 600;
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
  transition: all var(--animation-duration-normal) var(--animation-easing);
}

:deep(.ant-btn-primary:hover) {
  background: linear-gradient(135deg, #5a67d8 0%, #6b46c1 100%);
  box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
  transform: translateY(-2px);
}

/* 分割线样式 */
:deep(.ant-divider) {
  border-color: rgba(102, 126, 234, 0.2);
  margin: 24px 0;
}

/* 表格样式 */
.chat-table {
  background: transparent;
  border-radius: 16px;
}

:deep(.ant-table) {
  background: transparent;
  border-radius: 16px;
}

:deep(.ant-table-thead > tr > th) {
  background: rgba(102, 126, 234, 0.05);
  border-bottom: 1px solid rgba(102, 126, 234, 0.1);
  font-weight: 600;
  color: #374151;
  font-size: 14px;
}

:deep(.ant-table-tbody > tr) {
  transition: all var(--animation-duration-normal) var(--animation-easing);
}

:deep(.ant-table-tbody > tr:hover) {
  background: rgba(102, 126, 234, 0.02);
  transform: translateX(2px);
}

:deep(.ant-table-tbody > tr > td) {
  vertical-align: middle;
  border-bottom: 1px solid rgba(229, 231, 235, 0.5);
  font-weight: 500;
  color: #4b5563;
}

.message-text {
  max-width: 300px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 500;
  color: #374151;
  transition: all var(--animation-duration-normal) var(--animation-easing);
}

.message-text:hover {
  color: #667eea;
}

/* 标签样式 */
:deep(.ant-tag) {
  border-radius: 12px;
  font-weight: 500;
  padding: 4px 12px;
  border: none;
}

/* 分页样式 */
:deep(.ant-pagination) {
  margin-top: 24px;
  text-align: center;
}

:deep(.ant-pagination-item) {
  border-radius: 8px;
  border: 1px solid rgba(209, 213, 219, 0.8);
  transition: all var(--animation-duration-normal) var(--animation-easing);
}

:deep(.ant-pagination-item:hover) {
  border-color: #667eea;
  transform: translateY(-1px);
}

:deep(.ant-pagination-item-active) {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-color: #667eea;
}

:deep(.ant-pagination-item-active a) {
  color: white;
  font-weight: 600;
}

/* 响应式设计 */
@media (max-width: 768px) {
  #chatManagePage {
    padding: 20px 16px;
  }

  :deep(.ant-form) {
    padding: 16px;
  }

  :deep(.ant-table) {
    font-size: 12px;
  }

  .message-text {
    max-width: 200px;
  }
}
</style>
